package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.UserFollowDao;
import com.atguigu.entity.UserFollow;
import com.atguigu.service.DictService;
import com.atguigu.service.UserFollowService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 杨林
 * @create 2022-09-25 14:29 星期日
 * description:
 */
@Service(interfaceClass = UserFollowService.class)
@Transactional
public class UserFollowServiceImpl extends BaseServiceImpl<UserFollow> implements UserFollowService {

    @Autowired
    private UserFollowDao userFollowDao;

    @Reference
    private DictService dictService;

    @Override
    protected BaseDao<UserFollow> getEntityDao() {
        return userFollowDao;
    }


    /**
     * 根据userId 和 houseId 查找数据库
     * @param houseId
     * @param userId
     * @return
     */
    @Override
    public boolean isFollow(Long houseId, Long userId) {

        int count = userFollowDao.isFollow(houseId,userId);

        if (count > 0){
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<UserFollowVo> findListPage(Integer pageNum, Integer pageSize, Long userId) {

        //启用分页插件
        PageHelper.startPage(pageNum,pageSize);

        Page<UserFollowVo> page = userFollowDao.findListPage(userId);

        List<UserFollowVo> userFollowVoList = page.getResult();

        for (UserFollowVo userFollowVo : userFollowVoList) {
            String houseTypeName = dictService.getNameById(userFollowVo.getHouseTypeId());
            userFollowVo.setHouseTypeName(houseTypeName);
            String floorName = dictService.getNameById(userFollowVo.getFloorId());
            userFollowVo.setFloorName(floorName);
            String directionName = dictService.getNameById(userFollowVo.getDirectionId());
            userFollowVo.setDirectionName(directionName);
        }

        return new PageInfo<>(page.getResult());
    }
}
